import { featchMonitorSite } from 'src/api/siteMonitor'
import { getOranizerList } from 'src/api/asset'
import { getVersionsInfo } from 'src/api/systemtWarning'
const state = {
    initLevel: 0, // 菜单初始化缩进等级
    duration: 300, // 切换路由所需的时长
    visitedViews: [], // 可访问的视图
    assetId: null,
    siteId: null,
    allSnArr: [],
    devNameA: [],
    devLineObj: [],
    siteLineObj: '',
    devEchartId: '',
    channel: '',
    siteSn: [],
    siteEchartId: '',
    siteChannel: '',
    currentVersions: ''
}
const mutations = {
    SET_VISITED_VIEWS: (state) => {
        state.visitedViews = []
    },
    ADD_VISITED_VIEWS: (state, view) => { // some() 数组方法：只要数组中有一个符合条件的，返回true。和every() 功能相反
        if (view.meta.hidden) return
        if (state.visitedViews.some(v => v.path === view.path)) return
        state.visitedViews.push(
            Object.assign({}, view, {
                title: view.meta.title || 'no-name'
            })
        )
    },
    DEL_VISITED_VIEWS: (state, view) => {
        const visitedCopyView = state.visitedViews.entries()
        for (const [i, v] of visitedCopyView) {
            if (v.path === view.path) {
                state.visitedViews.splice(i, 1)
            }
        }
    },
    DEL_OTHERS_VISITED_VIEWS: (state, view) => {
        state.visitedViews = state.visitedViews.filter((v, index) => {
            return index === 0 || v.path === view.path
        })
    },
    DEL_CACHED_VIEW: (state, view) => {
        const index = state.visitedViews.indexOf(view.name)
        index > -1 && state.visitedViews.splice(index, 1)
    },
    DEL_ALL_VISITED_VIEWS: state => {
        const affixTages = state.visitedViews.filter((tag, index) => index === 0)
        state.visitedViews = affixTages
    },
    UPDATE_VISITED_VIEW: (state, view) => {
        for (let v of state.visitedViews) {
            console.log(v, '----- kkk ----')
            if (v.path === view.path) {
                v = Object.assign(v, view)
                break
            }
        }
    },


    SET_ASSET_ID: (state, assetId) => {
        state.assetId = assetId
    },
    SET_SITE_ID: (state, siteId) => {
        state.siteId = siteId
    },

    SET_ALL_SN_ARR: (state, assetInfo) => {
        state.allSnArr = []
        if (assetInfo.length > 0) {
            assetInfo.forEach(element => {
                state.allSnArr.push({ value: element.equip_sn, label: element.equip_sn, assetName: element.equip_name, assetId: element.id })
            })
        }
    },
    SET_SITE_SN: (state, data) => {
        state.siteSn = []
        if (data.length > 0) {
            data.forEach(element => {
                state.siteSn.push({
                    value: element.sn,
                    label: element.sn,
                    sn: element.sn,
                    siteId: element.id,
                    aliasName: element.alias_name,
                    type: element.type
                })
            })
        }
    },

    SET_DEV_LINE_OBJ: (state, idItem) => {
        state.devLineObj = idItem
    },
    SET_SITE_LINE_OBJ: (state, siteLineObj) => {
        state.siteLineObj = siteLineObj
    },
    SET_DEV_ECHART_ID: (state, devEchartId) => {
        state.devEchartId = devEchartId
    },
    SET_CHANNEL: (state, channel) => {
        state.channel = channel
    },

    SET_SITE_ECHART_ID: (state, siteEchartId) => {
        state.siteEchartId = siteEchartId
    },

    SET_SITE_CHANNEL: (state, siteChannel) => {
        state.siteChannel = siteChannel
    },
    CURRENT_VERSIONS: (state, versions) => {
        state.currentVersions = versions
    }
}

const actions = {
    // 添加所有的视图函数
    addView({ dispatch }, view) {
        dispatch('addVisitedView', view)
    },
    // 添加可访问的视图
    addVisitedView({ commit }, view) {
        commit('ADD_VISITED_VIEWS', view)
    },
    // 删除所有的视图
    delView({ dispatch, state }, view) {
        return new Promise(resolve => {
            dispatch('delVisitedViews', view)
            resolve({
                visitedViews: [...state.visitedViews]
            })
        })
    },
    // 删除可访问的视图
    delVisitedViews({ commit, state }, view) {
        return new Promise(resolve => {
            commit('DEL_VISITED_VIEWS', view)
            resolve([...state.visitedViews])
        })
    },
    // 删除其他可显示视图
    delOthersViews({ dispatch, state }, view) {
        return new Promise(resolve => {
            dispatch('delOthersVisitedViews', view)
                // dispatch('delOthersCachedViews', view)
            resolve({
                visitedViews: [...state.visitedViews],
                // cachedViews: [...state.cachedViews]
            })
        })
    },
    // 删除其他可访问的视图
    delOthersVisitedViews({ commit, state }, view) {
        return new Promise(resolve => {
            commit('DEL_OTHERS_VISITED_VIEWS', view)
            resolve([...state.visitedViews])
        })
    },
    // 刷新
    delCachedView({ commit, state }, view) {
        return new Promise(resolve => {
            commit('DEL_CACHED_VIEW', view)
            resolve([...state.visitedViews])
        })
    },
    // 删除所有
    delAllVisitedViews({ commit, state }) {
        return new Promise(resolve => {
            commit('DEL_ALL_VISITED_VIEWS')
            resolve([...state.visitedViews])
        })
    },
    updateVisitedView({ commit }, view) {
        commit('UPDATE_VISITED_VIEW', view)
    },

    getEquipList({ commit }, listQuery) {
        return new Promise((resolve, reject) => {
            getOranizerList(listQuery).then(res => {
                if (res.status === 200) {
                    const { data } = res
                    commit('SET_ALL_SN_ARR', data)
                }
                resolve(res)
            }).catch(error => {
                reject(error)
            })
        })
    },
    getSiteList({ commit }, listQuery) {
        return new Promise((resolve, reject) => {
            featchMonitorSite(listQuery).then(res => {
                if (res.status === 200) {
                    const { data } = res
                    commit('SET_SITE_SN', data)
                }
                resolve(res)
            }).catch(error => {
                reject(error)
            })
        })
    },

    // 获取版本号
    getVersion({ commit }) {
        return new Promise((resolve, reject) => {
            getVersionsInfo().then(res => {
                commit('CURRENT_VERSIONS', res.data.version)
                resolve()
            })
        })
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}